
import {
    HTTP_REQUEST_URL,
    HEADER,
    TOKENNAME,
    TIMEOUT
} from '@/config/app';
import {
    toLogin,
    checkLogin
} from '../libs/login';
import store from '../store';

/**
 * 发送请求
 */
function baseRequest(url, method, data, {
    noAuth = false,
    noVerify = false
}) {
    let Url = HTTP_REQUEST_URL,
        header = HEADER;

    if (!noAuth) {
        //登录过期自动登录
        if (!store.state.token && !checkLogin()) {
            toLogin();
            return Promise.reject({
                msg: '未登录'
            });
        }
    }
    if (store.state.token) header['token'] = store.state.token;

    return new Promise((reslove, reject) => {
        uni.request({
            url: Url + '/' + url,
            method: method || 'GET',
            header: header,
            data: data || {},
            timeout: TIMEOUT,
            success: (res) => {
                if (noVerify)
                    reslove(res.data, res);
                else if (res.data.code == 0)
                    reslove(res.data, res);
                else if ([110002, 110003, 110004].indexOf(res.data.code) !== -1) {
                    toLogin();
                    reject(res.data);
                } else if (res.data.code == 1) {
                    uni.showModal({
                        title: '提示',
                        content: res.data.message,
                        showCancel: false,
                        confirmText: '我知道了'
                    });
                } else
                    reject(res.data.msg || '系统错误');
            },
            fail: (msg) => {
                let data = {
                    mag: '请求失败',
                    status: 1 //1没网
                }
                // #ifdef APP-PLUS
                reject(data);
                // #endif
                // #ifndef APP-PLUS
                reject('请求失败');
                // #endif
            }
        })
    });
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
    request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});



export default request;
